package com.packagetracker.utils;

import android.content.ContentValues;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.packagetracker.carriers.PackageLocation;
import com.packagetracker.entities.Settings;
import com.packagetracker.helpers.SettingsHelper;
import com.packagetracker.providers.TrackerProviderSetup;
import com.packagetracker.views.TrackerMap;

public class MapUtil {
	
	public static void moveTo(final MapView view, final PackageLocation location, final int zoom) {
		final MapController controller = view.getController();
		controller.animateTo(location.getGeoPoint());
		controller.setZoom(zoom);
	}
	
	public static Drawable getMarker(final MapView view, final int icon) {
		final Drawable marker = view.getResources().getDrawable(icon);
		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
		return(marker);
	}
	
	public static void toggleZoomControls(final MapView view) {
		final LinearLayout zoom = (LinearLayout)view.getZoomControls();
		zoom.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
		zoom.setGravity(Gravity.BOTTOM + Gravity.CENTER_HORIZONTAL);
		
		view.displayZoomControls(true);
		view.addView(zoom);
	}
	
	public static void toggleSatellite(final MapView view, final int type) {
		view.setSatellite(type == Settings.TYPE_SATELLITE);
	}
	
	public static boolean keyEvents(final TrackerMap map, final int key, final KeyEvent event) {
		final ContentValues values = new ContentValues();
		
		switch (key) {
			case KeyEvent.KEYCODE_I:
				map.getView().getController().setZoom(map.getView().getZoomLevel() + 1);
				return true;
				
			case KeyEvent.KEYCODE_O:
				map.getView().getController().setZoom(map.getView().getZoomLevel() - 1);
				return true;
				
			case KeyEvent.KEYCODE_S:
				values.put(TrackerProviderSetup.Settings.MAP_TYPE, Settings.TYPE_SATELLITE);
				new SettingsHelper(map).update(values);
				
				MapUtil.toggleSatellite(map.getView(), Settings.TYPE_SATELLITE);
				return true;
				
			case KeyEvent.KEYCODE_T:
				values.put(TrackerProviderSetup.Settings.MAP_TYPE, Settings.TYPE_MAP);
				new SettingsHelper(map).update(values);
				
				MapUtil.toggleSatellite(map.getView(), Settings.TYPE_MAP);
				return true;
		}
		
		return false;
	}
	
}